#Step 0: Prepare your system

#Install git and virtualenv:
sudo apt install git python-virtualenv

#Install the build dependencies for Python 2.7.13:
sudo apt-get install build-essential libsqlite3-dev zlib1g-dev libncurses5-dev libgdbm-dev libbz2-dev libreadline-gplv2-dev libssl-dev libdb-dev

#Install the build dependencies for pygame:
sudo apt-get install libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsdl1.2-dev libsmpeg-dev python-numpy subversion libportmidi-dev ffmpeg libswscale-dev libavformat-dev libavcodec-dev libv4l-dev python-dev mercurial

#We need to set one symbolic link to make sure that pygame can be compiled correctly.
cd /usr/include/linux
sudo ln -s ../libv4l1-videodev.h videodev.h
cd ~

#Step 1: Setup the building enviorment

#First we need to declare some variables:
APP=Labyrinth
LOWERAPP=${APP,,}
VERSION=0.1.1
ARCH=i686

#Now we have to get a little script which will provide us some extra functionalities for the bash.
wget -q https://github.com/probonopd/AppImages/raw/master/functions.sh -O ./functions.sh

#By default this script is made for x86_64 mashines. So, if we want to build our AppImage for another platform we need to do an extra step.
sed -i -e 's/x86_64/$ARCH/g' functions.sh

#Now we can run the script.
. ./functions.sh

#And now we create some directories.

mkdir -p $APP/$APP.AppDir/usr
cd $APP/$APP.AppDir

#Next we can get Python 2.7.13 and compile it.

wget http://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz
tar -xzf Python-2.7.13.tgz
rm Python-2.7.13.tgz
cd Python-2.7.13

./configure --prefix=$HOME/$APP/$APP.AppDir/usr #the prefix is important for this whole thing to work
make && make install

cd ..
rm -r Python-2.7.13

#Now we add pip to our installation
wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py
$HOME/$APP/$APP.AppDir/usr/bin/python ./get-pip.py
rm ./get-pip.py


#Now we should have a working Python installation inside ./usr
#We can check this by typing: ./usr/bin/python

 
#In the next step we will add pygame to our local Python installtion.

hg clone https://bitbucket.org/pygame/pygame
cd pygame

$HOME/$APP/$APP.AppDir/usr/bin/python ./setup.py build
$HOME/$APP/$APP.AppDir/usr/bin/python ./setup.py install

cd ..
rm -r pygame

#At this point we should have a working Python installation which includes pygame.
#Maybe it would be a good idea to make a backup uf this.

#Now let us add the game itself.

git clone --depth 1 https://gitlab.com/ericxdu/labyrinth-demo
cp -r ./labyrinth-demo/* ./usr/bin
rm -r -f ./labyrinth-demo
chmod a+x ./usr/bin/play.py #I am not sure if this really needs to be done.

#Step 2: Building the AppImage

#First let us create another little shellscript. This one will be part of our Appimage.
#Inside this script we can set enviorment variables or pass options to your programm (like in this little example).
cat > usr/bin/$LOWERAPP <<\EOF
#!/bin/sh
play.py --fullscreen
EOF

chmod a+x usr/bin/$LOWERAPP

#Now we use one of the new functions to get an AppRun executable.
get_apprun

#Next we have to generate a dektop entry.
cat > $LOWERAPP.desktop <<EOF
[Desktop Entry]
Name=$APP
Exec=$LOWERAPP
Icon=$LOWERAPP
Category=Game

Comment=A labyrinth game by Eric Duhamel.
EOF

#With this command we allow the AppImage to ask for desktop integration.
get_desktopintegration $LOWERAPP

#Now we need a icon file.
cp usr/bin/data/Game_Icon.png $LOWERAPP.png

#Time to let the magic happen!
copy_deps ; copy_deps ; copy_deps
move_lib
LIBPULSE=$(find -name libpulsecommon-9.0.so)
PARENT=${LIBPULSE/'pulseaudio/libpulsecommon-9.0.so'/}
cp $LIBPULSE $PARENT
cd ..
generate_appimage
